package com.yangxk.kadmin.auth.filter;

import com.yangxk.kadmin.auth.service.CoreService;
import com.yangxk.kadmin.common.exception.NoPermissionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * 权限过滤器
 * 并未被使用
 * @author yangxk
 * @date 2018/9/9 14:16
 */
@Deprecated
public class AuthFilter implements Filter{

    @Autowired
    private CoreService coreService;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException{
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        String path = request.getServletPath();
        if (coreService.isPass(path, request)) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        throw new NoPermissionException(coreService.userId(request) + "无权限访问url:" + path);

    }

    @Override
    public void destroy() {

    }
}
